<template>
    <div class="login">
        <h2>SHUN-Z</h2>
        <h3><router-link to="/register">注册</router-link></h3>
        <router-link to="/" class="back">主页</router-link>
        <div class="form">
            <div class="account">
                <p>账号：</p>
                <input v-model="account" type="text" placeholder="请输入账号">
            </div>
            <div class="password">
                <p>密码：</p>
                <input v-model="password" type="text" placeholder="请输入密码">
            </div>
            <div class="other">
                <p>忘记密码？</p>
                <p>邮箱验证登录</p>
            </div>
            <button class="btn" @click="login">登录</button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router';
import { ref } from 'vue';
import { showToast } from 'vant';
import api from '../api/request';
const $router = useRouter()

let account = ref('')
let password = ref('')

const login = () => {
    if(account.value === '' || password.value === ''){
        showToast({
            message: '账号或密码不能为空'
        })
    }
    else{
       api.login({account: account.value, password: password.value})
       .then(res=>{
        if(res.code==200){
            // 保存本地token
            localStorage.setItem('token',res.token);
            showToast(res.msg)
            // 跳转主页
            $router.push('/')
        }
        else{
            showToast(res.msg)
        }
       })
    }
}

</script>

<style lang="less" scoped>
@import url(../assets/appColor.less);

.login {
    position: absolute;
    width: 100%;
    height: 100vh;
    padding: 15px;
    background: linear-gradient(to bottom right, #f7e0aa, #cb86d9);
    .back {
        position: absolute;
        top: 10px;
        left: 10px;
        font-size: 20px;
        font-weight: bold;
    }
    h3 {
        position: absolute;
        top: 10px;
        right: 10px;

        a {
            font-size: 20px;
        }
    }

    h2 {
        margin-top: 30%;
        text-align: center;
        margin-left: 50%;
        transform: translateX(-50%);
        margin-bottom: 15%;
        font-size: 25px;
        font-family: Fantasy;
    }

    .form {
        box-shadow: 5px 5px 10px 0 rgba(0, 0, 0, 0.5);
        border-radius: 15px;
        padding: 10px;
        display: flex;
        flex-direction: column;
        align-items: center;

        >div {
            height: 35px;
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            width: 90%;
            align-items: center;

            input {
                border-bottom: 1px solid #757575;
                width: 80%;
                padding: 5px;
            }
        }

        .other {
            width: 90%;

            p {
                color: #757575;
                font-size: 12px;
            }
        }

        .btn {
            font-size: 18px;
            font-weight: bold;
            color: @TColor1;
            background-color: @color2;
            padding: 10px 20px;
            width: 90%;
            border-radius: 15px;
        }
    }
}
</style>